using System.Text;

namespace NFreeMarker.Core
{
    /**
     * Represents a case in a switch statement.
     */
    public sealed class Case : TemplateElement // NOTE: Was internal
    {
        // might as well just make these package-visible 
        // so the Switch can use them, no need to be too anal-retentive
        internal readonly bool IsDefault;
        internal readonly Expression Expression;

        internal Case(Expression expression, TemplateElement nestedBlock, bool isDefault)
        {
            Expression = expression;
            NestedBlock = nestedBlock;
            IsDefault = isDefault;
        }

        internal override void Accept(Environment env)
        {
            if (NestedBlock != null)
            {
                env.Visit(NestedBlock);
            }
        }

        public override string GetCanonicalForm()
        {
            var buf = new StringBuilder();
            if (IsDefault)
            {
                buf.Append("<#default>");
            }
            else
            {
                buf.Append("<#case ");
                buf.Append(Expression.GetCanonicalForm());
                buf.Append(">");
            }
            if (NestedBlock != null)
            {
                buf.Append(NestedBlock.GetCanonicalForm());
            }
            return buf.ToString();
        }

        public override string GetDescription()
        {
            if (IsDefault)
            {
                return "default case";
            }
            return "case " + Expression;
        }
    }
}